package com.timmy.google_play.activity;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;

import com.timmy.google_play.R;
import com.timmy.google_play.base.LoadingPager;
import com.timmy.google_play.bean.ItemBean;
import com.timmy.google_play.holder.detail.DetailBottomHolder;
import com.timmy.google_play.holder.detail.DetailDesHolder;
import com.timmy.google_play.holder.detail.DetailInfoHolder;
import com.timmy.google_play.holder.detail.DetailPicHolder;
import com.timmy.google_play.holder.detail.DetailSafeHolder;
import com.timmy.google_play.manager.DownLoadInfo;
import com.timmy.google_play.manager.DownLoadManager;
import com.timmy.google_play.protocol.DetailProtocol;
import com.timmy.google_play.utils.CheckUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DetailActivity extends AppCompatActivity {

    @BindView(R.id.flBottomContainer)
    FrameLayout mFlBottomContainer;
    @BindView(R.id.flInfoContainer)
    FrameLayout mFlInfoContainer;
    @BindView(R.id.flSafeContainer)
    FrameLayout mFlSafeContainer;
    @BindView(R.id.flPicContainer)
    FrameLayout mFlPicContainer;
    @BindView(R.id.flDesContainer)
    FrameLayout mFlDesContainer;
    private String             mPackageName;
    private LoadingPager       mLoadingPager;
    private ItemBean           mDatas;
    private DetailBottomHolder mBottomHolder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_detail);

        init();
        initView();
        initData();
    }


    /**
     * 初始化,接收意图传递过来的参数
     */
    private void init() {
        mPackageName = getIntent().getStringExtra("packagename");

        //Toast.makeText(getApplicationContext(), mPackageName, Toast.LENGTH_SHORT).show();
    }

    /**
     * 初始化View,决定Activity可以展示的视图情况
     */
    private void initView() {

        mLoadingPager = new LoadingPager(this) {

            /**
             * 子线程中加载数据
             */
            @Override
            public LoadedResult initData() {
                return DetailActivity.this.onInitData();
            }

            /**
             * 决定成功视图是什么
             */
            @Override
            public View initSuccessView() {
                return DetailActivity.this.initSuccessView();
            }
        };

        //调用 setContentView
        setContentView(mLoadingPager);
    }

    /**
     * 初次创建的时候,加载数据
     */
    private void initData() {
        mLoadingPager.triggerLoadData();
    }

    /**
     * 子线程中加载数据
     */
    private LoadingPager.LoadedResult onInitData() {
        SystemClock.sleep(500);

        //得到数据,加载成功视图.
        DetailProtocol protocol = new DetailProtocol(mPackageName);

        try {
            mDatas = protocol.loadData(0);
            return CheckUtils.checkResData(mDatas);
        } catch (Exception e) {
            e.printStackTrace();
            return LoadingPager.LoadedResult.ERROR;
        }
    }

    /**
     * 返回加载成功的视图是什么
     */
    private View initSuccessView() {

        View successView = View.inflate(this, R.layout.layout_detail, null);
        ButterKnife.bind(this, successView);

        //应用信息部分
        DetailInfoHolder infoHolder = new DetailInfoHolder();
        mFlInfoContainer.addView(infoHolder.holdView);
        infoHolder.setDataAndRefreshHolderView(mDatas);

        //应用安全部分
        DetailSafeHolder safeHolder = new DetailSafeHolder();
        mFlSafeContainer.addView(safeHolder.holdView);
        safeHolder.setDataAndRefreshHolderView(mDatas);

        //应用截图部分
        DetailPicHolder picHolder = new DetailPicHolder();
        mFlPicContainer.addView(picHolder.holdView);
        picHolder.setDataAndRefreshHolderView(mDatas);

        //应用描述部分
        DetailDesHolder desHolder = new DetailDesHolder();
        mFlDesContainer.addView(desHolder.holdView);
        desHolder.setDataAndRefreshHolderView(mDatas);

        //应用的下载部分
        mBottomHolder = new DetailBottomHolder();
        mFlBottomContainer.addView(mBottomHolder.holdView);
        mBottomHolder.setDataAndRefreshHolderView(mDatas);

        //添加观察者到观察者集合当中
        DownLoadManager.getInstance().addObserver(mBottomHolder);

        return successView;
    }

    @Override
    protected void onResume() {

        //动态添加观察者到集合中
        if (mBottomHolder != null) {
            DownLoadManager.getInstance().addObserver(mBottomHolder);

            //手动发布最新的状态
            DownLoadInfo downLoadInfo = DownLoadManager.getInstance().getDownLoadInfo(mDatas);
            DownLoadManager.getInstance().notifyObserver(downLoadInfo);

        }

        super.onResume();
    }

    @Override
    protected void onPause() {
        //动态移除观察者到集合中
        if (mBottomHolder != null) {
            DownLoadManager.getInstance().deleteObserver(mBottomHolder);
        }

        super.onPause();
    }
}
